package com.browan.constellation.db;

import java.util.ArrayList;
import java.util.List;
import com.browan.constellation.data.RSSData;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;

public class DbHelper extends DbConfig
{

	private DataBase help;
	private SQLiteDatabase mDb;
	private Context mContext;

	public DbHelper(Context c)
	{
		this.mContext = c;
		open();
	}

	private class DataBase extends SQLiteOpenHelper
	{

		public DataBase(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db)
		{
			db.execSQL(CREATE_TABLE_RSS_ITEM);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
		{
			db.execSQL("DROP TABLE IF EXISTS " + ITEM_TABLE);
			onCreate(db);
		}
	}

	public DbHelper open() throws SQLiteException
	{
		help = new DataBase(mContext);
		mDb = help.getWritableDatabase();
		return this;
	}

	public void close()
	{
		mDb.close();
		help.close();
	}

	public void execute(String sql)
	{
		mDb.execSQL(sql);
	}

	public Cursor query(String sql)
	{

		return null;
	}

	public void deleteTable()
	{
		execute("delete from " + ITEM_TABLE + " where " + ITEM_ISSAVE + "=0;");
	}

	public void insertItem(RSSData data)
	{
		ContentValues values = new ContentValues();

		values.put(ITEM_TITLE, data.getTitle());

		values.put(ITEM_DESC, data.getDescription());

		values.put(ITEM_PUBDATE, data.getPubDate());

		values.put(ITEM_ISSAVE, data.getSave());

		values.put(ITEM_UPDATE_TIME, data.getUptime());

		values.put(ITEM_LINK, data.getLink());

		values.put(ITEM_CATEGORY, data.getCategory());

		mDb.insert(ITEM_TABLE, null, values);
	}

	public void historyItem(int id)
	{
		ContentValues values = new ContentValues();
		values.put(ITEM_ISSAVE, 1);
		mDb.update(ITEM_TABLE, values, _ID + "=?", new String[] { String.valueOf(id) });
	}

	public void unHistory(RSSData data)
	{
		int id = data.getId();
		if (data.getUptime().equals(RSSData.CURRENT_RSS_UPTIME))
		{
			unHistory(id);
		}
		else
		{
			mDb.delete(ITEM_TABLE, _ID + "=" + id, null);
		}
	}

	private void unHistory(int id)
	{
		ContentValues values = new ContentValues();
		values.put(ITEM_ISSAVE, 0);
		mDb.update(ITEM_TABLE, values, _ID + "=?", new String[] { String.valueOf(id) });
	}

	public List<RSSData> getItemList(String date)
	{
		List<RSSData> list = new ArrayList<RSSData>();
		Cursor cursor = mDb.query(ITEM_TABLE, null, ITEM_UPDATE_TIME + "='" + date + "'", null, null, null, null);
		if (cursor.moveToFirst())
		{
			do
			{
				RSSData data = new RSSData(cursor);
				list.add(data);
			}
			while (cursor.moveToNext());
		}
		close(cursor);
		return list;
	}

	public void updateRssItem(List<RSSData> datas)
	{
		String sql = "insert into " + ITEM_TABLE + "(" + ITEM_TITLE + "," + ITEM_CATEGORY + "," + ITEM_DESC + ","
				+ ITEM_ISSAVE + "," + ITEM_LINK + "," + ITEM_PUBDATE + "," + ITEM_UPDATE_TIME
				+ ")values(?,?,?,?,?,?,?)";
		mDb.beginTransaction();

		try
		{
			deleteTable();

			SQLiteStatement stat = mDb.compileStatement(sql);

			for (RSSData data : datas)
			{

				stat.bindString(1, data.getTitle());

				stat.bindString(2, data.getCategory());

				stat.bindString(3, data.getDescription());

				stat.bindLong(4, data.getSave());

				stat.bindString(5, data.getLink());

				stat.bindString(6, data.getPubDate());

				stat.bindString(7, data.getUptime());

				stat.executeInsert();
			}

			stat.close();
			mDb.setTransactionSuccessful();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			mDb.endTransaction();
		}
		finally
		{

			mDb.endTransaction();
		}

	}

	public Cursor getItemCurson(String date)
	{
		Cursor cursor = mDb.query(ITEM_TABLE, null, ITEM_UPDATE_TIME + "='" + date + "'", null, null, null, null);
		return cursor;
	}

	public Cursor getHistoryCurson()
	{
		return mDb.query(ITEM_TABLE, null, ITEM_ISSAVE + "=1", null, null, null, null);
	}

	private void close(Cursor c)
	{
		if (null != c)
		{
			c.close();
			c = null;
		}
	}
	
	public void removeOldLuckData(String date){
		String sql = "delete from " + ITEM_TABLE + " where " + ITEM_PUBDATE + "='" + date + "' and " + ITEM_ISSAVE +"=2;";
		execute(sql);
	}

	public void insertLuck(String title, String des, String date,int reta)
	{

		ContentValues values = new ContentValues();

		values.put(ITEM_TITLE, title);

		values.put(ITEM_PUBDATE, date);

		values.put(ITEM_DESC, des);

		values.put(ITEM_LINK, String.valueOf(reta));

		values.put(ITEM_ISSAVE, 2);

		mDb.insert(ITEM_TABLE, null, values);
	}

	public Cursor qureyLuck(String date)
	{
		return mDb.query(ITEM_TABLE, new String[] { ITEM_DESC, ITEM_TITLE, ITEM_LINK }, ITEM_ISSAVE + "=2 and "
				+ ITEM_PUBDATE + "='" + date + "'", null, null, null, null);
	}
}